#!/bin/ksh93
#
# set -vx
# ----------------------------------------------------------------------
# Note:
# During developement, this file is sometimes edited in a Windows editor.
# In case the ^M (Carriage Return) is still in the file, use the
# following command to correct this error:
#   tr -d '\r' <cf*sh >cfg2html_aix_2.72_CORR.sh
# now make the new file executable:
#   chmod +x cfg2html_aix_2.72_CORR.sh
# try to run it:
#   ./cfg2html_aix_2.72_CORR.sh
# if everything is ok now, you can delete the corrupt one,
# and rename the new file to the original name.
# ----------------------------------------------------------------------
# 2002-10-15 GL 1.0  Initial release
# 2003-02-24 GL 2.2e xxx
# 2003-05-09 GL 2.5a xxx
# 2005-05-13 AW 2.6  some improvements/bugfixes
# 2005-09-30 GL 2.6c xxx
# 2005-10-01 CP 2.5b <private release>
# 2005-10-07 AW 2.7  new release number, HP OpenView support
# 2008-01-24 AW 2.71 new Option -J for JAVA checkings
# 2008-07-24 AW 2.72 add Java6 check, using /tmp/c2h
# ----------------------------------------------------------------------

RCCS="@(#)Cfg2Html -AIX- Version 2.72"      # useful for what (1)
VERSION=$(echo $RCCS | cut -c5-)

echo "Starting up $VERSION\r"

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# RR Ralph Roth Ralph_Roth@hp.com
# GL Gert Leerdam Gert.Leerdam@getronics.com
# AW Andreas Wizemann Andreas.Wizemann@FVVaG.de
# CP Chris Paulheim (HP OpenView support)
# MP Michel Philipona (Veritas VCS, Veritas VxVM, Hitachi HDLM)
#
# X.xx 200x-xx-xx ?????????
#  - remove support for AIX 5.2 (End of Service September 2008)
#  - niminv (NIM Master only)
#  - add basic Oracle checkings
#  - aixpert
#  - more AIX 6 support
#  - HTML Navigation (AddHTML)
#    <a name="TOP"></a>
#    <a href="#TOP">TOP</a>
#  - check for latest version of Mozilla, Firefox
#
# 2.72 2008-07-24 A.Wizemann
#  - some bugs fixed
#  - internal improvements
#  - remove support for AIX 5.1 (End of Service April 2005)
#  - remove pmctrl (bos.powermgt.rte)
#  - remove support for AIX 4.3.3 (End of Service December 2003)
#  - no longer tested with AIX 5.2 and below !
#  - add check for Java6
#  - update checks for Java141 and Java5
#  - use /tmp/c2h as temp directory
#  - remove Java 118,122,130 checkings
#  - Java 131 now shown as unsupported
#  - IBM ESS (Shark) moved from sdd to seperate block
#  - add tunables files to list of files (vmo, ioo)
#  - vmo,ioo moved from EXPERIMENTAL to KERNEL
#  - suma,emgr,wlm moved from EXPERIMENTAL to KERNEL
#  - smtctl,cpustat,mpstat,lparstat moved from EXPERIMENTAL to KERNEL
#  - show warning if system needs to be rebootet (rc=1 from vmo)
#  - ssh,rpm moved from EXPERIMENTAL to SOFTWARE
#
# 2.71 2008-01-24 A.Wizemann
#  - AIX 6 Toleration
#  - Power6 Toleration
#  - geninv
#  - use ksh93
#  - some bugs fixed
#  - improved Java checkings
#  - enhanced OS Level checkings (ML/TL+SP)
#  - show TSM 64-Bit BA Client config files (dsm.opt, dsm.sys)
#  - add pagesize command
#  - add bindprocessor command
#  - experimental support FAStT
#  - experimental support EMC PowerPath
#  - experimental support HDS/Hitachi
#  - experimental support Veritas
#  - experimental support HACMP
#
# 2.7 2005-10-07 A.Wizemann
#  - some bugs fixed
#  - internal improvements
#
# 2.6 2005-05-13 A.Wizemann
#  - some bugs fixed
#  - internal improvements
#  - new: AIX 5.3 support
#  - new: ported some options from LINUX or HP version
#
# New
# --------
# >MP001< Veritas VCS, Veritas VxVM, Hitachi HDLM
#
# New
# --------
# >CP001< HP OpenView support (copied from 2.5b)
# >CP002< TSM 32-Bit Client
#
# BUGFIXES
# --------
# >GL001< AIX 5.3 correction in psawk
# >GL002< additions from 2.5c
#
# changes and general improvements
# --------------------------------
# >AW001< Translate German comments to english
# >AW002< Translate Dutch comments to english
# >AW003< add some more comments
# >AW004< always use option -f and 2>/dev/null on rm command
# >AW005< changed var RECHNER to NODE / ANTPROS to NO_OF_CPUS
# >AW006< code to identify CPU Type moved to function and improved
# >AW007< add more debuging capabilities
# >AW008< code restructured for better readability
# >AW009< changed tabs to spaces for better readability in some editors
# >AW010< changed internel name device_info to disk_info to fit with option name (CFG_DISKS)
# >AW011< changed internel name patch/software_info to software_info to fit with option name (CFG_SOFTWARE)
# >AW012< add CFG_APPL (APPLICATIONS e.g SAMBA) (as in linux version)
# >AW013< display warning if using AIX 4.x as this has reached end of service
# >AW014< AIX 5.3 use sysdump -Lv older releases -L
# >AW015< small corrections in HTML header to show correct oslevel
# >AW016< moved <META http-equiv=expires... to open_html, so it is only found once in the html file
# >AW017< TRAP handling
# >AW018< xexit - single exit point
# >AW019< add support for "sysinfo" file. Idea "stolen" from linux port
# >AW020< -o OUTDIR support (as in HP Version 2.92)
# >AW021< C2H_CMDLINE (same as CFG_CMDLINE in HP Version 2.92)
# >AW022< C2H_DATE    (same as CFG_DATE in HP Version 2.92)
# >AW023< get info about package to install if cmd not found
# >AW024< check for command availability using "which <cmd>" before using them
# >AW025< add /etc/security/limits to files
# >AW026< add (internal) css style (as in LINUX 1.20)
# >AW027< add ALT= option to IMG tag
# >AW028< repquota (10) moved to "Filesystem" collector
# >AW029< passwd (13) moved to "User & Group" collector
# >AW030< defrag (11) moved to "Filesystem" collector
# >AW031< screen tips inline (same as CFG_STILINE in HP Version 2.92)
# >AW032< AIX 4.2 and lower desupported
# >AW033< small optical changes in output files
# >AW034< show arp cache
# >AW035< add CFG_JAVA (move Java to its own collector)
# >AW036< AIX 6 toleration
# >AW037< Power6 toleration
# >AW038< show page size for paging
# >AW039< bindprocessor -q
# >AW040< TSM 64-Bit BA Client
# >AW041< suppress error messgaes from which command
# >AW042< check if dsm.sys exists
# >AW043< tcbcheck removed
# >AW044< proctree -a needs userid
# >AW045< use /tmp/c2h for temp files
# >AW046< update Java checks
# >AW047< add niminv
# >AW048< add basic Oracle checkings
# >AW049< add /etc/tunables/* to files shown
# >AW050< suma (AIX 5.2 with PTF or AIX 5.3 and higer;)
# >AW051< *emgr,epkg (AIX 5.2 + IY40236 or AIX 5.3 and higher)
# >AW000< ...052 used for EXPERIMENTAL JAVA
# >AW053< *WLM* Work Load Manager (AIX 5.x)
# >AW054< smtctl (AIX 5.3 on Power5)
# >AW055< cpupstat (AIX 5.3)
# >AW056< mpstat -s (AIX 5.3)
# >AW057< lparstat (AIX 5.3)
# >AW000< ...058-064 used for EXPERIMENTAL
# >AW065< vmo,ioo
# >AW000< ...066 used for EXPERIMENTAL JAVA
# >AW067< ssh
# >AW000< ...068 used for EXPERIMENTAL JAVA
# >AW069< rpm
# >AW000< ...070-075 used for EXPERIMENTAL
# >AW076< show sys0 attributes
# >AW077< HTML-NAV
# >AW078< show time needed for this script
# >AW079< add .rhosts, .profile, .kshrc to list of files
# >AW080< update Java checks (Java5 SR7 *)
# >AW081< update Java checks (Java142 SR11 *)
# >AW082< aio (async io) check on AIX6
# >AW000< ...083
#
# Under Construction
# ------------------
# >AW104< add "PLUGIN" support
# >AW105< check that we are running only once
#
# >AW201< check for "vmtune" 5.2 knows vmtune IF UPGRADED from earlier release 5.3 uses vmo,ioo,schedo
# >AW202< skip "ps -Af..." on all aix (5.3)
# >AW203< skip "ipcs" on AIX 5.3 (aixn,...)
#
# NEW (EXPERIMENTAL)
# ---
# >AW052< *java -version
# >AW058< lsslot
# >AW059< aio, ASYNC I/O
# >AW060< *SDD/SDDPCM* Subsystem Device Driver (ESS, DS8000), CIM-Agent
# >AW061< *IBM HTTP Server (Apache)
# >AW062< *SAMBA
# >AW063< Java5 checking
# >AW064< links to internet pages (e.g. IBM's Java page)
# >AW066< *FAStT*
# >AW068< gcc
# >AW070< *HDS/Hitachi*
# >AW071< *EMC/PowerPath*
# >AW072< *Veritas*
# >AW073< *HACMP*
# >AW074< *IBM ESS*
# >AW075< AIX 6 support
#
# BUGFIXES
# --------
# >AW301< BUG: var DB_F in PrtLayout not set
# >AW302< BUG: do not use fix "proc0" ! as we may run on a different proc
# >AW303< BUG: sed will convert also for *.TXT output file, which is not necessary
# >AW303< BUG: found in: cron,lsrset,ps,ifconfig,netstat
# >AW304< BUG: change uname -n to $(uname -n)
# >AW305< BUG: do not display "<file> file not found" in *.err file !
# >AW306< BUG: ignore newly created devices which do not have a PVID (PVID is none)
# >AW307< BUG: check for "/var/ifor/i4cfg" Don't use if not available !
# >AW308< BUG: lower "f" missing in getopts list
# >AW309< BUG: if execution fails or scrpt is interrupted output is missing !
# >AW310< BUG: out of memory error (first seen on AIX 5.3)
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#*********************************************************************
# TODO: alstat  emstat  gennames  truss  wimmon alog?
# TODO: errpt -a bij -x
# ToDo: needs improvement!
# ToDo: lppcheck in BG (files in /tmp/c2h
# trap "echo Signal: Aborting!; rm $HTML_OUTFILE_TEMP"  2 13 15
#*********************************************************************

#----------------------------------------------------------------------------
# Thanks to:
# Olaf Morgenstern (olaf.morgenstern@web.de) for several improvements
# Jim Lane (JLane@torontohydro.com) for lsuser & lsgroup command
# "Stolen" Command-line option structure with getopts from HP-UX version from Ralph Roth
# Marco Stork for supplying the PrtLayout function
# Antonio Ferreira for the HDS/Hitachi commands
#----------------------------------------------------------------------------

######################################################################
# usage: show the options for this script
# Collectors use CAPITAL letters to enable/disable.
######################################################################
usage ()
{
   echo "\n usage: cfg2html_aix.sh [options]"
   echo "\ncreates HTML and plain ASCII host documentation"
   echo
   echo "  -o  set directory to write (or use the environment variable)"
   echo "                OUTDIR=\"/path/to/dir\" (directory must exist)"
  #echo "  -l  DIS-able: Screen tips inline"  # >AW031<
  #         ToDo. -g like LINUX port
  #         x in use with eXtended. use g (gif)
  #echo "  -g  donxt create background images (gif)"
   echo "  -0 (null)     append the current date+time to the output files (D-M-Y-hhmm)"  # >AW022<
   echo "  -1 (one)      append the current date to the output files (Day-Month-Year)"   # >AW022<
  #echo "  -2 modifier   like option -1, you can use date +modifer, e.g. -2%d%m"         # >AW022<
  #echo "                DO NOT use spaces for the filename, e.g. -2%c"                  # >AW022<
   echo "  -h  display this help and exit"
   echo "  -v  output version information and exit"
   echo "  -x  eXtended output"
   echo "  -y  Verbose (Debug) output"
   echo
   echo "use the following (case-sensitive) options to (enable/)disable collectors"
   echo
   echo "  -^  Reverse Yes/No; MUST be first option"
   echo "  -a  DIS-able: Applications (e.g. SAMBA)" # *col-18*
   echo "  -c  DIS-able: VCS"            # *col-25* >MP001<
   echo "  -C  DIS-able: Cron"           # *col-12*
   echo "  -d  DIS-able: Defragfs"       # *col-11* >AW030< => to be DELETED
   echo "  -D  DIS-able: Disks"          # *col-05*
   echo "  -e  DIS-able: Enhancements"   # *col-19*
   echo "  -E  DIS-able: Experimental"   # *col-20*
   echo "  -f  DIS-able: Files"          # *col-15*
   echo "  -F  DIS-able: Filesystem"     # *col-04*
   echo "  -H  DIS-able: Hardware"       # *col-03*
   echo "  -J  DIS-able: JAVA"           # *col-22* >AW035<
   echo "  -K  DIS-able: Kernel"         # *col-02*
   echo "  -l  DIS-able: LUM"            # *col-17*
   echo "  -L  DIS-able: LVM"            # *col-06*
   echo "  -m  DIS-able: VxVM"           # *col-23* >MP001<
   echo "  -M  DIS-able: HDLM"           # *col-24* >MP001<
   echo "  -n  DIS-able: NIM"            # *col-16*
   echo "  -N  DIS-able: Network"        # *col-08*
   echo "  -O  DIS-able: HP OpenView"    # *col-21* >CP001<
   echo "  -p  DIS-able: Password"       # *col-13* >AW029< => to be DELETED
   echo "  -P  DIS-able: Printer"        # *col-09*
   echo "  -Q  DIS-able: Quota"          # *col-10* >AW028< => to be DELETED
   echo "  -s  DIS-able: Software"       # *col-14*
   echo "  -S  DIS-able: System"         # *col-01*
   echo "  -U  DIS-able: Users"          # *col-07*

   echo
 # echo  "\n(#) these collectors create a lot of information!"
   echo  "Example:  $0 -C   to skip CRON"
   echo  "          $0 -^C  to do -ONLY- CRON"
   echo
}

######################################################################
# InitVars: initialize some basic variables
######################################################################
InitVars ()
{
#----------------------------------------------------------------------------
# use "yes/no" to enable/disable a collection; CASE sensitive !!
 CFG_SYSTEM=yes          # S  *col-01*
 CFG_KERNEL=yes          # K  *col-02* ! Trouble on 5.3 ! (psawk)
 CFG_HARDWARE=yes        # H  *col-03*
 CFG_FILESYS=yes         # F  *col-04*
 CFG_DISKS=yes           # D  *col-05* (Device)
 CFG_LVM=yes             # L  *col-06*
 CFG_USERS=yes           # U  *col-07*
 CFG_NETWORK=yes         # N  *col-08*
 CFG_PRINTER=no          # P  *col-09*
 CFG_QUOTA=yes           # Q  *col-10* >AW028< => to be DELETED
 CFG_DEFRAG=yes          # d  *col-11* >AW030< => to be DELETED
 CFG_CRON=yes            # C  *col-12*
 CFG_PASSWD=yes          # p  *col-13* >AW029< => to be DELETED
 CFG_SOFTWARE=yes        # s  *col-14* (Patch)
 CFG_FILES=yes           # f  *col-15* ! Trouble on 5.3 !
 CFG_NIM=yes             # n  *col-16*
 CFG_LUM=yes             # l  *col-17*
 CFG_APPL=yes            # a  *col-18* comming soon
 CFG_ENH=yes             # e  *col-19* comming soon
 CFG_EXP=yes             # E  *col-20*
 CFG_HPO=yes             # O  *col-21* >CP001<
 CFG_JAVA=yes            # J  *col-22* >AW035<
 CFG_VxVM=yes            # m  *col-23* >MP001<
 CFG_HDLM=yes            # M  *col-24* >MP001<
 CFG_VCS=yes             # c  *col-25* >MP001<
 maxcoll=25

 CFG2HTML=true
 MYSELF=`whence $0`
 C2H_HOME=`dirname $MYSELF`
 PLUGINS=${C2H_HOME}/plugins

 C2H_DATE="";  # >AW022<
 C2H_STINLINE="YES"  # >AW031<

# Convert illegal characters for HTML into escaped ones.
# Convert '&' first! (Peter Bisset [pbisset@emergency.qld.gov.au])
CONVSTR='
s/&/\&amp;/g
s/</\&lt;/g
s/>/\&gt;/g
s/\\/\&#92;/g
'

# ...
 SEP="================================" # 32
 SEP10="==========" #10
 SEP20="====================" #20
 SEP30=${SEP20}""${SEP10}
 SEP40=${SEP20}""${SEP20}
 SEP60=${SEP40}""${SEP20}
 SEP70=${SEP40}""${SEP20}""${SEP10}
 SEP80=${SEP40}""${SEP40}
 SEP90=${SEP40}""${SEP40}""${SEP10}
 SEP100=${SEP40}""${SEP40}""${SEP20}
 SEP120=${SEP40}""${SEP40}""${SEP40}
#
 DATE=$(date "+%Y-%m-%d")                 # ISO8601 compliant date string
 DATEFULL=$(date "+%Y-%m-%d - %H:%M:%S")  # ISO8601 compliant date and time string
 CURRDATE=$(date +"%b %e %Y")
 NODE=$(uname -n) # >AW005<
 SYSTEM=$(uname -s)
 USER=`id | cut -f2 -d"(" | cut -f1 -d")"`

# Let the (HTML) cache expire since this script runs every night
 EXPIRE_CACHE=$(date "+%a, %d %b %Y ")"23:00 GMT"

# >AW017< Define signals
 SIGNEXIT=0   ; export SIGNEXIT  # normal exit
 SIGHUP=1     ; export SIGHUP    # when session disconnected
 SIGINT=2     ; export SIGINT    # ctrl-c
 SIGTERM=15   ; export SIGTERM   # kill command
 SIGSTP=18    ; export SIGSTP    # ctrl-z
#SIG...=13    ; export SIG...    # ...

# get no of installed CPU's
 NO_OF_CPUS=$(lscfg | grep 'proc[0-9]' | awk 'END {print NR}')

}

######################################################################
# Init_Part2: called AFTER options are processed
######################################################################
Init_Part2 ()
{
# Todo: write to *.err file (after file is available !)
# AWTRACE "AW SET"
#  set | grep -e CFG_ -e OUTDIR_ENV
# AWTRACE "AW SET"

# ToDo: CONF
# c2h.conf
# var=123
# var=$(grep var $CONF|awk -F"="{print $1}")
#

 if [ "$OUTDIR" = "" ] ; then
   OUTDIR="."  # >AW020<
 fi
 AWTRACE "AW000I OUTDIR=${OUTDIR}"
 AWTRACE "AW000I OUTDIR_ENV=${OUTDIR_ENV}"

 AWTRACE "AW000I TERM=${TERM}"
 AWTRACE "AW000I LANG=${LANG}"
 AWTRACE "AW000I DISPLAY=${DISPLAY}"

#++ >AW045< start *TMPDIR*---------------------------------------------
#...Check if /tmp/c2h dir is there
 TMPDIR=/tmp/c2h
 if [ ! -d ${TMPDIR} ]
 then
   echo "C2H011I Tmp-Dir = $TMPDIR"
   echo "C2H010W Warning, the tmp directory is missing or execute bit is not set"
   mkdir $TMPDIR
   mkdir_rc=$?
   AWTRACE     "C2H022I mkdir rc $mkdir_rc"
   case $mkdir_rc in
     0 ) # OK
         echo "C2H012I Tmp-Dir sucessfully created";
         ;;
     * )
         echo "C2H013E ERROR creating Tmp-Dir ! rc=$mkdir_rc";
         ;;
   esac
 fi
#++ >AW045< end *TMPDIR* ----------------------------------------------

#++ >AW105< start *run-once*-------------------------------------------
AWCONS "AWTRACE: CBR 003  (run-once) MYSELF=${MYSELF} 0=${0} B=${BASEFILE} P=${PROCID}"
# ToDo: BASEFILE not set
# ps -ef | grep -v ${PROCID} | grep -v grep | grep ${BASEFILE}
  ps -ef | grep ${PROCID} | grep -v grep | grep -v "ps -ef"

AWCONS "AWTRACE: CBR 004 B=${BASEFILE} P=${PROCID}"

#If only one instance of a script should run at a given time,
#a lock file could be used to ensure mutual exclusion:

lockfile=${TMPDIR}/cfg2html.lock
if touch "$lockfile"
then
    echo ${PROCID} >>${lockfile}
    echo >&2 "C2H070I aquired lock; continuing"
    # Remove lock file when script terminates
    trap 'rm "$lockfile"' 0
    trap "exit 2" 1 2 3 15
else
    echo >&2 "C2H071I lock already held"
    echo >&2 "C2H072I another script instance running?"
    echo >&2 "C2H003I delete lockfile $lockfile manually"
    exit 1
fi

# TODO:
#This can be extended to e.g. write the current process id to a file in the
#lock directory. This way another instance can check if the process that
#aquired the lock is still running.
AWCONS "AWTRACE: CBR 005 "$BASEFILE $PROCID


#++ >AW105< end *run-once* --------------------------------------------

 BASEFILE=`hostname`$C2H_DATE  # >AW022<
 ERROR_LOG=${OUTDIR}/${BASEFILE}.err

 HTML_OUTFILE=${OUTDIR}/${BASEFILE}.html
 HTML_OUTFILE_TEMP=${TMPDIR}/${BASEFILE}.html.$$
 TEXT_OUTFILE=${OUTDIR}/${BASEFILE}.txt
 TEXT_OUTFILE_TEMP=${TMPDIR}/${BASEFILE}.txt.$$

#++ >AWccc< end *cleanupe* --------------------------------------------
AWCONS "AWTRACE: CCC 001 ls"
 ls -la /tmp/${BASEFILE}.html.[0-9]* 2>/dev/null
 rc=$?
 if [[ $_rc = 0 ]]
 then
   :  # OK
 else
   AWTRACE "C2H000I ls /tmp/x.html RC=${rc}"
   TEXT_STATUS="ERROR"
 fi
 ls -la /tmp/${BASEFILE}.txt.[0-9]*  2>/dev/null
 ls -la /tmp/exec_cmd.tmp.[0-9]* 2>/dev/null

 ls -la ${TMPDIR}/${BASEFILE}.html.[0-9]* 2>/dev/null
 rc=$?
 if [[ $_rc = 0 ]]
 then
   :  # OK
 else
   AWTRACE "C2H000I ls /tmp/x.html RC=${rc}"
   TEXT_STATUS="ERROR"
 fi
 ls -la ${TMPDIR}/${BASEFILE}.txt.[0-9]* 2>/dev/null
 ls -la ${TMPDIR}/exec_cmd.tmp.[0-9]* 2>/dev/null

AWCONS "AWTRACE: CCC 002 rm"
# remove dump
 rm -f /tmp/${BASEFILE}.html.[0-9]* 2>/dev/null
 rm -f ${TMPDIR}/${BASEFILE}.html.[0-9]* 2>/dev/null
 rm -f /tmp/${BASEFILE}.txt.[0-9]* 2>/dev/null
 rm -f ${TMPDIR}/${BASEFILE}.txt.[0-9]* 2>/dev/null
 rm -f /tmp/exec_cmd.tmp.[0-9]* 2>/dev/null
 rm -f ${TMPDIR}/exec_cmd.tmp.[0-9]* 2>/dev/null
AWCONS "AWTRACE: CCC 003 xx"
#++ >AWccc< end *cleanupe* --------------------------------------------

 if [ ! -d $OUTDIR ] ; then
  echo "cannot create ${HTML_OUTFILE}, ${OUTDIR} does not exist"
  xexit 1
 fi

 cmdout=$(touch $HTML_OUTFILE 2>/dev/null)
 touch_rc=$?
 if [[ $touch_rc = 0 ]]
 then
   :  # OK
   HTML_STATUS="TOUCH"
 else
   banner "Error"
   ERRMSG "C2H007S Cannot create ${HTML_OUTFILE} RC=${touch_rc}"
   HTML_STATUS="ERROR"
   xexit 1
 fi

 cmdout=$(touch $TEXT_OUTFILE 2>/dev/null)
 touch_rc=$?
 if [[ $touch_rc = 0 ]]
 then
   :  # OK
   TEXT_STATUS="TOUCH"
 else
   banner "Error"
   ERRMSG "C2H007S Cannot create ${TEXT_OUTFILE} RC=${touch_rc}"
   TEXT_STATUS="ERROR"
   xexit 1
 fi

# clear error_log
 [ -s "$ERROR_LOG" ] && rm -f $ERROR_LOG 2> /dev/null

 ERRMSG ${DATEFULL}" "${VERSION} # First entry in new *.err file
 AWTRACE "User...: ${USER}"
 AWTRACE "ProcID.: ${PROCID}"
 AWTRACE "TERM...: ${TERM}"
 AWTRACE "MYSELF.: ${MYSELF}"
 AWTRACE "PLUGINS: ${PLUGINS}"

 check_os

 check_basic_req  # check for some basic requirements...

# ToDo: check if dir exist before adding to path
# set Path to be used in this script
 PATH00=$PATH:/local/bin:/local/sbin:/usr/bin:/usr/sbin:/local/gnu/bin:/usr/ccs/bin:
 PATH01=/local/X11/bin:/usr/openwin/bin:/usr/dt/bin:/usr/proc/bin:/usr/ucb:
 PATH02=/local/misc/openv/netbackup/bin
 PATH=${PATH00}""${PATH01}""${PATH03}

 DATEFULL=$(date "+%Y-%m-%d - %H:%M:%S")     # ISO8601 compliant date and time string
 AWTRACE "DT-0 "${DATEFULL}
# may take up to 30 sec
 get_ProcessorInfo

 DATEFULL=$(date "+%Y-%m-%d - %H:%M:%S")     # ISO8601 compliant date and time string
 AWTRACE "DT-1 "${DATEFULL}
 get_rsct_csm_Info

 DATEFULL=$(date "+%Y-%m-%d - %H:%M:%S")     # ISO8601 compliant date and time string
 AWTRACE "DT-2 "${DATEFULL}
}

######################################################################
# check_os: check for correct (supported) os level and dependancies
######################################################################
check_os ()  # >AW018<
{
# OSLEVEL checks
# --------------
 OSLEVEL=$(oslevel)      # V.R.M.x
 OSLEVEL_R=$(oslevel -r) # VRMx-ML

 os_vr="$(uname -v)$(uname -r)"
 os_v="$(uname -v)"

#----------------------------------------------------------
 AWTRACE "OSLEVEL_R="${OSLEVEL_R}

 typeset -L2 ml # use 2 char from left (AIX6 Beta gives "XX BETA")

 osl=$(echo ${OSLEVEL_R} | cut -d "-" -f1) # e.g. 5300
 ml=$(echo ${OSLEVEL_R} | cut -d "-" -f2) # e.g. 04
#----------------
# 6.1.0 ALWAYS TL
# 6.0.0 ALWAYS TL (AIX6 Beta)
# 5.3.0 ML4=TL4
# 5.2.0 ML8=TL8
#----------------
 mltl="ML" # ML or TL
 case $osl in
   6100 ) mltl="TL"
          ;;
   6000 ) mltl="TL"
          ;;
   5300 )
      case ${ml} in
       00 ) mltl="ML";;
       01 ) mltl="ML";;
       02 ) mltl="ML";;
       03 ) mltl="ML";;
        * ) mltl="TL";;
      esac;
          ;;
   5200 )
      case ${ml} in
       00 ) mltl="ML";;
       01 ) mltl="ML";;
       02 ) mltl="ML";;
       03 ) mltl="ML";;
       04 ) mltl="ML";;
       05 ) mltl="ML";;
       06 ) mltl="ML";;
       07 ) mltl="ML";;
        * ) mltl="TL";;
      esac;
          ;;
   * )
     echo "C2H030I Unknown AIX Level";
 esac;

 OSLEVEL_S=0000-00-00 # init
 case $mltl in
   "ML" ) AWTRACE "You are at MAINTENANCE LEVEL ${ml}"
          ;;
   "TL" ) AWTRACE "You are at TECHNOLOGY  LEVEL ${ml}"
          OSLEVEL_S=$(oslevel -s) # VRMx-ML-SP
          ;;
   * )
     echo "C2H031E Unexpected value ${mltl} for var mltl";
 esac;
 echo ""

 AWTRACE "Node...: ${NODE}"

# OSLEVEL    => 5.3.0.0
# OSLEVEL_S  => 5300-04-01
# OSLEVEL_R  => 5300-04
# OSVER6CHAR => 530004
# OSVER3CHAR => 530
# os_v       => 5
# os_vr      => 53

 if test oslevel
 then
   OSVER6CHAR=`oslevel -r | sed 's/-//'`
   OSVER3CHAR=`oslevel -r | awk '{print substr($1,1,3)}'`
 else
   OSVER6CHAR="000000"
   OSVER3CHAR="000"
 fi

  if [ "$os_vr" -gt 61 ] ; then  # >AW036<
     banner "Warning"
     echo "C2H032I Unsupported AIX Version! Please contact developer!\n"
     xexit 1 # >AW018<
  fi

  if [ "$os_vr" -eq 62 ] ; then  # >AW...<
     echo "C2H033I Note: This Version of AIX has NEVER been testet with this version of the script !\n"
    #exit 1 # >AW018<
  fi

  if [ "$os_vr" -eq 52 ] ; then  # >AW...<
     echo "C2H034W ATTENTION: Version 2.72 of this script is the last to support AIX 5.2 !\n"
     echo "C2H035I Note: This Version of AIX has NOT been testet with this version of the script !\n"
    #exit 1 # >AW018<
  fi

  if [ "$os_vr" -lt 52 ] ; then  # >AW032<
     banner "Sorry"
     echo "C2H036E Requires AIX 5.2 or better!\n"
     xexit 1 # >AW018<
  fi

 case $OSLEVEL in
   6.2.0.0 ) # OK
             AWCONS "CURRENTLY NOT TESTESD ON AIX 6.2"
             ;;
   6.0.0.0 ) # OK
             AWCONS "WARNING NOT FULLY TESTESD ON AIX 6 BETA"
             ;;
   5.2.0.0 ) # OK
             AWCONS "WARNING NOT TESTESD ON AIX 5.2"
             ;;
         * ) # OK
             AWCONS "C2H037I Supported OS \"${OSLEVEL}\" "
             ;;
 esac

}

######################################################################
# showOSver: show the vars containing os level info
######################################################################
showOSver ()  # >AW...<
{
# SHOW OSLEVEL
# ------------
 AWCONS "OSLEVEL: ${1}"
 AWCONS "os_v       = ${os_v}"
 AWCONS "os_vr      = ${os_vr}"
 AWCONS "OSLEVEL    = ${OSLEVEL}"
 AWCONS "OSLEVEL_S  = ${OSLEVEL_S}"
 AWCONS "OSLEVEL_R  = ${OSLEVEL_R}"
 AWCONS "OSVER6CHAR = ${OSVER6CHAR}"
 AWCONS "OSVER3CHAR = ${OSVER3CHAR}"
 AWCONS "mltl       = ${mltl}"
}

######################################################################
# check_basic_req: check for some basic requirements...
######################################################################
check_basic_req ()
{
# echo "\n"
#AWCONS "AWTRACE: CBR 000"
  if [ $(id -u) != 0 ] ; then
     banner "Sorry"
     line
     echo "You must run this script as Root\n"
     xexit 1 # >AW018<
  fi

#AWCONS "AWTRACE: CBR 001 "$HTML_OUTFILE
  if [ ! -f $HTML_OUTFILE ]
  then
     banner "Error"
     echo "C2H008S You have not the rights to create ${HTML_OUTFILE}! (NFS?)\n"
     xexit 1 # >AW018<
  fi
#AWCONS "AWTRACE: CBR 002"

#++ >AW104< start *PLUGIN*---------------------------------------------
#...Check if /plugins dir is there
  PLUGINS=${C2H_HOME}/plugins
  if [ ! -d ${PLUGINS} ]
  then
    echo "C2H700I Plugin-Dir = $PLUGINS"
    echo "C2H701W Warning, the plugin directory is missing or execute bit is not set"
#   xexit 1 # >AW018<
    mkdir $PLUGINS
    mkdir_rc=$?
    AWTRACE     "C2H009I mkdir rc $mkdir_rc"
    case $mkdir_rc in
      0 ) # OK
          echo "C2H702I Plugin-Dir sucessfully created";
          ;;
      * )
          echo "C2H703E ERROR creating Plugin-Dir ! rc=$mkdir_rc";
          ;;
    esac
  fi
#++ >AW104< end *plugin* ----------------------------------------------

#++ >AW310< start *out of memory*--------------------------------------
# 128K - 131.072
# 256K - 262.144
# 384K - 393.216
# 512K - 524.288
#...Check if ulimit -d is at least 524288
 curr_ulimit_d=$(ulimit -d) # -d => data area
 if [[ $curr_ulimit_d = "unlimited" ]]
 then
   verbose_out "C2H020I ulimit -d is set to 'unlimited'. No action required."
   AWTRACE     "C2H020I ulimit -d is set to 'unlimited'. No action required."
 elif [[ $curr_ulimit_d -lt 524288 ]]
 then
   verbose_out "C2H020I ulimit -d was "$curr_ulimit_d" now set to 524288"
   AWTRACE     "C2H020I ulimit -d was "$curr_ulimit_d" now set to 524288"
   ulimit -d 524288  # set the new ulimit
 else
   verbose_out "C2H020I ulimit -d is set to "$curr_ulimit_d". No action required."
   AWTRACE     "C2H020I ulimit -d is set to "$curr_ulimit_d". No action required."
 fi

 curr_ulimit_c=$(ulimit -c) # -c => core dumps
 AWTRACE "C2H021I ulimit -c is "$curr_ulimit_c
 curr_ulimit_f=$(ulimit -f) # -f => file size
 AWTRACE "C2H021I ulimit -f is "$curr_ulimit_f
 curr_ulimit_m=$(ulimit -m) # -m => memory
 AWTRACE "C2H021I ulimit -m is "$curr_ulimit_m
 curr_ulimit_n=$(ulimit -n) # -n => number of file descriptors
 AWTRACE "C2H021I ulimit -n is "$curr_ulimit_n
 curr_ulimit_s=$(ulimit -s) # -s => stack
 AWTRACE "C2H021I ulimit -s is "$curr_ulimit_s
 curr_ulimit_t=$(ulimit -t) # -t => number of seconds to be used by each proc
 AWTRACE "C2H021I ulimit -t is "$curr_ulimit_t

# new ulimits in AIX6: threads, processes  # >AW075<
 case $osl in
   6100 ) # new ulimits
          curr_ulimit_r=$(ulimit -r) # -r => number of threads a process can have
          AWTRACE "C2H021I ulimit -r is "$curr_ulimit_r
          curr_ulimit_u=$(ulimit -u) # -u => number of process a user can create
          AWTRACE "C2H021I ulimit -u is "$curr_ulimit_u
          ;;
   * ) # nop
          ;;
 esac;

#++ >AW310< end *out of memory*----------------------------------------

#++ >AW019< start *SYSINF*---------------------------------------------
#...Check if file *SYSINF* is there
# SYSINF=c2h_sysinfo.txt
# if [ ! -f $SYSINF ]
# then
#   echo "C2H030W Warning, the sysinfo file '${SYSINF}' is missing. Using internal defaults"
# else
#   cat $SYSINF
# fi
#++ >AW019< end *SYSINF* ---------------------------------------------

AWCONS "AWTRACE: CBR 999"
}

######################################################################
# getProcessorInfo: called AFTER basc requirements
######################################################################
get_ProcessorInfo ()
{
 Get_CPU_Type  # >AW006<

#----------------------------------------------------------
# IPADRES=$(cut -d"#" -f1 /etc/hosts | awk '{for (i=2; i<=NF; i++) if ("'$HOSTNAME'" == $i) {print $1; exit}}') # n.u.
#----------------------------------------------------------
# SPEED=$(psrinfo -v | awk '/MHz/{print $(NF-1); exit }')    # TODO: lots of work ! (silly IBM ?) ....
#SPEED=XXX     # TODO: ...
#----------------------------------------------------------
# CPU=$(lscfg | grep Architecture | cut -d: -f2)     # n.u.
#----------------------------------------------------------

# set PROC_Type e.g. PowerPC_POWER4
# some commands may be available only on specific hardware
# e.g. smt (Simultaneous Multi-Threading) with AIX 5.3 on POWER5
# TODO check only first proc (all procs are the same !)
# TODO Machine has n-proc / LPAR has n-proc
 procs=$(lscfg | grep proc | awk '{print $2}')
 for proc in $(echo $procs)

 do
   proctype=$(lsattr -El $proc | grep type | awk '{print $2}')
 done
 AWTRACE ": PROC_Type="$proctype
 POWER5="NO"
 if [[ $proctype = "PowerPC_POWER5" ]] ; then
   POWER5="YES"
 fi
 AWTRACE ": POWER5="$POWER5

# >AW037< Power6 toleration
 POWER6="NO"
 if [[ $proctype = "PowerPC_POWER6" ]] ; then
   POWER5="YES" # Power6 has same functionality than Power5
   POWER6="YES"
 fi
 AWTRACE ": POWER6="$POWER6

# NOTE prtconf without options runs about 5 sec

# set SysModel e.g. IBM,7038-6M2 (p650)
 SysModel=$(prtconf 2>/dev/null | grep "System Model" | awk '{print $3}')
 AWTRACE ": SysModel="$SysModel
 AWCONST ": PRTCONF1"

# set PROC_Type e.g. PowerPC_POWER4
 proctype2=$(prtconf 2>/dev/null | grep "Processor Type" | awk '{print $3}')
 AWTRACE ": PROC_Type2="$proctype2
 AWCONST ": PRTCONF2"

# set CPU_Type e.g. 64-bit
 CPU_TYPE=$(prtconf -c | grep "CPU Type" | awk '{print $3}')
 AWTRACE ": CPU_Type="$CPU_TYPE

# set KERNEL_Type e.g. 64-bit
 KERNEL_TYPE=$(prtconf -k | grep "Kernel Type" | awk '{print $3}')
 AWTRACE ": KERNEL_Type="$KERNEL_TYPE

 BIT64="NO"
 if [[ $KERNEL_TYPE = "64-bit" ]] ; then
   BIT64="YES"
 fi
 AWTRACE ": 64BIT="$BIT64

 if [[ "$KERNEL_TYPE" = "$CPU_TYPE" ]]
 then
   :
   #ERRMSG "\nC2H050 I OK ! You are running a ${KERNEL_TYPE}-Kernel on a ${CPU_TYPE}-CPU !"
 else
   ERRMSG "\nC2H051W Warning ! You are running a ${KERNEL_TYPE}-Kernel on a ${CPU_TYPE}-CPU !"
 fi

}

######################################################################
# getRSCT_CSM_Info: ...
######################################################################
get_rsct_csm_Info ()
{
# check RSCT
  RSCT="INIT"
  fileset="rsct.core.sr"
  lslpp -l ${fileset} 2>/dev/null | grep -i ${fileset} |uniq|awk '{print $2}'|while read vv
  do
    rsct_installed=${vv}
  done
#
  rsct_base_u=$(lslpp -l rsct.core.sr)
  rsct_base=$(lslpp -l rsct.core.sr >/dev/null 2>&1)
  rsct_rc=$?
  #ERRMSG "RSCT_BASE RC=${rsct_rc}"
  case $rsct_rc in
  0 )
    RSCT="YES"
    AWTRACE "C2H100I RSCT installed: ${rsct_installed}";
    #AWTRACE "\n${rsct_base_u}";
    ;;
  1 )
    RSCT="NO"
    AWTRACE "C2H101W RSCT NOT installed RC=${rsct_rc}";
    ;;
  * )
    RSCT="UNKNOWN"
    AWTRACE "C2H102E RSCT error RC=${rsct_rc}";
    ;;
  esac

# check CSM
  CSM="INIT"
  fileset="csm.core"
  lslpp -l ${fileset} 2>/dev/null | grep -i ${fileset} |uniq|awk '{print $2}'|while read vv
  do
    csm_installed=${vv}
  done
#
  csm_base_u=$(lslpp -l csm.core)
  csm_base=$(lslpp -l csm.core >/dev/null 2>&1)
  csm_rc=$?
  #ERRMSG "CSM_BASE RC=${csm_rc}"
  case $csm_rc in
  0 )
    CSM="YES"
    AWTRACE "C2H100I CSM installed: ${csm_installed}";
    #AWTRACE "${csm_base_u}";
    ;;
  1 )
    CSM="NO"
    AWTRACE "C2H101W CSM NOT installed RC=${csm_rc}";
    ;;
  * )
    CSM="UNKNOWN"
    AWTRACE "C2H102 CSM error RC=${csm_rc}";
    ;;
  esac

}

######################################################################
# xexit: extended exit (e.g. run cleanup)
######################################################################
xexit ()  # >AW018<
{
 if [ -z "$1" ] ; then      # if string 1 is zero/empty (no rc given)
   #ERRMSG "C2H951I no rc at call to xexit. Set to ZERO."
   xrc=0
 else
   xrc=$1
 fi

# write HTML_OUTFILE_TEMP to HTML_OUTFILE !!
 close_html

 cleanup

 DATEFULL=$(date "+%Y-%m-%d - %H:%M:%S")     # ISO8601 compliant date and time string
 echo "Finished ${VERSION} at ${DATEFULL}. RC=${xrc}";

 exit $xrc
}

######################################################################
# cleanup: remove some files
######################################################################
cleanup ()
{
 CLEAN=CLEAN+1
 echo ${SEP120} >> $ERROR_LOG
 ERRMSG "C2H950I now performing cleanup...(${CLEAN})"

# list process
 AWCONST "PROCID=${PROCID}"
 ps -ef | grep $PROCID | grep -v grep | grep -v "ps -ef"
 echo "======"
 ERRMSG ps -ef | grep $PROCID | grep -v grep

# list temp files for this process
 echo "======"
 touch ${TMPDIR}/awaw_${PROCID}
 ERRMSG ls -la ${TMPDIR}/*$$

# remove dump
 rm -f core 2>/dev/null  # >AW004<

 # remove the error.log if it has size zero
 [ ! -s "$ERROR_LOG" ] && rm -f $ERROR_LOG 2> /dev/null

 END_HMS=$(date "+%H:%M:%S")  # >AW078<
 TimeDiff ${START_HMS} ${END_HMS} # >AW078<
}

######################################################################
# TimeDiff: calc diff of 2 times # >AW078<
######################################################################
TimeDiff ()
{
 integer seconds
 integer hh1 mm1 ss1 time1
 integer hh2 mm2 ss2 time2

# convert time to seconds

 echo "${1}" | IFS=":" read hh1 mm1 ss1
 echo "${2}" | IFS=":" read hh2 mm2 ss2

 (( time1 = hh1*3600 + mm1*60 + ss1 ))
 (( time2 = hh2*3600 + mm2*60 + ss2 ))

# get diff in seconds
 (( seconds = time1 - time2 ))
 (( seconds < 0 )) && (( seconds = -seconds ))

 ERRMSG "C2H090I Duration in seconds: ${seconds}"

# convert diff to hh:mm:ss
 (( hh1 = seconds / 3600 ))
 (( mm1 = seconds / 60 % 60 ))
 (( ss1 = seconds % 60 ))

 RT=$(printf "C2H091I Runtime %02d:%02d:%02d\n" $hh1 $mm1 $ss1)
 ERRMSG ${RT}
}

######################################################################
# line: ...
######################################################################
line ()
{
# ToDo: tput not available in dtterm and xterm
  echo "--=[ "$(tput smso)"http://cfg2html.sourceforge.net"$(tput sgr0)" ]=------------------------------------------------"
}

######################################################################
# txtline: ...
######################################################################
txtline ()
{
  echo "--=[ http://cfg2html.sourceforge.net ]=------------------------------------------------"
}

######################################################################
# DBG: write DEBUG information
######################################################################
DBG ()
{
 if (( $DEBUG == 1 )); then
   DATEFULL=$(date "+%Y-%m-%d - %H:%M:%S")     # ISO8601 compliant date and time string
   # tee -a will "add" the output to a file
   dbgline="${DATEFULL} $*"
  #echo $dbgline | tee -a $ERROR_LOG
   echo $dbgline >> $ERROR_LOG
 fi
}

######################################################################
# AWCONS: write line to console
######################################################################
AWCONS ()
{
 if (( $AWDEBUG == 1 )); then
  echo "$*"
 fi
}

######################################################################
# AWCONST: write line to console (WITH TIMESTAMP)
######################################################################
AWCONST ()
{
 if (( $AWDEBUG == 1 )); then
  DATEFULL=$(date "+%Y-%m-%d - %H:%M:%S")     # ISO8601 compliant date and time string
  echo "${DATEFULL} $*"
 fi
}

######################################################################
# AWTRACE: write line to error log
######################################################################
AWTRACE ()
{
 if (( $AWDEBUG == 1 )); then
  DATEFULL=$(date "+%Y-%m-%d - %H:%M:%S")     # ISO8601 compliant date and time string
  echo "${DATEFULL} $*" >> $ERROR_LOG
 fi
}

######################################################################
# ERRMSG: write line to error log AND console
######################################################################
ERRMSG ()
{
  echo "$*"
  echo "$*" >> $ERROR_LOG
}

######################################################################
# verbose_out: display text if verbose is on
######################################################################
verbose_out ()
{
  if (( VERBOSE == 1 )) ; then
    echo "$*"
  fi
}

#*********************************************************************
# start of HTML file with heading and titel
#
# Note: there is only ONE HTML and TXT file after the script has
#       finished. This file contains an index and all command output,
#       so this file might be quite large.
#       While this script is running we are writing the index
#       (directory)" entries directly to the main output file.
#       Command output is written to a temp file, which will then
#       be copied to the end of the main output file when the script
#       finishes.
#*********************************************************************

######################################################################
# open_html: ...
#####################################                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             